package com.cc.openeye.dynamic.kuikly.recommend

import com.cc.openeye.dynamic.kuikly.recommend.items.banner.BannerItemView
import com.cc.openeye.dynamic.kuikly.recommend.items.dynamic.DynamicItemView
import com.cc.openeye.dynamic.kuikly.recommend.items.loadMore.InternalLoadMoreItemView
import com.cc.openeye.dynamic.kuikly.recommend.items.loadMore.LoadMoreItemView
import com.cc.openeye.dynamic.kuikly.recommend.items.square.SquareItemView
import com.cc.openeye.dynamic.recommend.entity.LoadMoreStatus
import com.cc.openeye.dynamic.recommend.entity.dynamicResponse.Item
import com.cc.openeye.dynamic.recommend.items.RecommendDynamicItemHelper.ITEM_TYPE_BANNER
import com.cc.openeye.dynamic.recommend.items.RecommendDynamicItemHelper.ITEM_TYPE_DYNAMIC
import com.cc.openeye.dynamic.recommend.items.RecommendDynamicItemHelper.ITEM_TYPE_SQUARE
import com.tencent.kuikly.core.base.ComposeView
import com.tencent.kuikly.core.base.ComposeAttr
import com.tencent.kuikly.core.base.ComposeEvent
import com.tencent.kuikly.core.base.ViewBuilder
import com.tencent.kuikly.core.base.ViewContainer
import com.tencent.kuikly.core.coroutines.delay
import com.tencent.kuikly.core.coroutines.launch
import com.tencent.kuikly.core.directives.vforIndex
import com.tencent.kuikly.core.manager.PagerManager
import com.tencent.kuikly.core.views.WaterfallList

internal class InternalRecommendView : ComposeView<RecommendViewAttr, RecommendViewEvent>() {

    private val recommendViewModel = KuiklyRecommendViewModel.newInstance(this)
    private var listWidth = 0f
    private var internalLoadMoreItemView: InternalLoadMoreItemView? = null
    private var isInCodeTime = false

    override fun created() {
        super.created()
        listWidth = pagerData.pageViewWidth - 30
        loadData("created")
    }

    private fun loadData(sence: String) {
        if (isInCodeTime) return
        isInCodeTime = true
        PagerManager.getCurrentPager().lifecycleScope.launch {
            recommendViewModel.requestDynamicData(sence)
            val requestColdTime = 300
            delay(requestColdTime)
            isInCodeTime = false
        }
    }

    override fun createEvent() = RecommendViewEvent()

    override fun createAttr() = RecommendViewAttr()

    override fun body(): ViewBuilder {
        val ctx = this
        return {
            attr {
                width(pagerData.pageViewWidth)
                paddingLeft(15f)
                paddingRight(15f)
            }

            WaterfallList {
                attr {
                    flex(1f)
                    columnCount(2)
                    listWidth(ctx.listWidth)
                    lineSpacing(10f)
                    itemSpacing(10f)
                    ctx.recommendViewModel.loadMoreStatus.also {
                        when (it) {
                            LoadMoreStatus.HaveMore -> ctx.internalLoadMoreItemView?.havaMoreData()
                            LoadMoreStatus.NoMore -> ctx.internalLoadMoreItemView?.notMoreData()
                            LoadMoreStatus.LoadErr -> ctx.internalLoadMoreItemView?.loadError()
                            LoadMoreStatus.NONE -> {}
                        }
                    }
                }
                event {
                    scroll { scrollParams ->
                        if (scrollParams.contentHeight - scrollParams.offsetY < ctx.pagerData.pageViewHeight * 1.2) {
                            ctx.loadData("preLoad")
                        }
                        println("====>scrollParams: ${scrollParams.contentHeight}, ${scrollParams.offsetY}, ${scrollParams.contentHeight - scrollParams.offsetY}, ${ctx.pagerData.pageViewHeight}")
                    }

                }
                ctx.buildListItems().invoke(this)
                ctx.buildLoadMoreView().invoke(this)
            }
        }
    }

    private fun buildLoadMoreView(): ViewBuilder {
        val ctx = this
        return {
            LoadMoreItemView {
                ctx.internalLoadMoreItemView = this
                attr {
                    width(ctx.listWidth)
                }
                event {
                    onLoadMore = {
                        ctx.loadData("loadMore")
                    }
                }
            }
        }
    }

    private fun buildListItems(): ViewBuilder {
        val ctx = this
        return {
            vforIndex({ ctx.recommendViewModel.datas }) { item, _, _ ->
                when (item.getItemType()) {
                    ITEM_TYPE_SQUARE -> ctx.buildSquareItemView(item).invoke(this)
                    ITEM_TYPE_BANNER -> ctx.buildBannerItemView(item).invoke(this)
                    ITEM_TYPE_DYNAMIC -> ctx.buildDynamicItemView(item).invoke(this)
                }
            }
        }
    }

    private fun buildSquareItemView(item: Item): ViewBuilder {
        val ctx = this
        return {
            SquareItemView {
                attr {
                    width(ctx.listWidth)
                    itemList = item.data?.itemList
                }
            }
        }
    }

    private fun buildBannerItemView(item: Item): ViewBuilder {
        val ctx = this
        return {
            BannerItemView {
                attr {
                    width(ctx.listWidth)
                    itemList = item.data?.itemList
                    height = 180f
                    width = ctx.listWidth
                }
            }
        }
    }

    private fun buildDynamicItemView(item: Item): ViewBuilder {
        val ctx = this
        return {
            DynamicItemView {
                attr {
                    data = item.data
                    listWidth = ctx.listWidth
                }
            }
        }
    }

}


internal class RecommendViewAttr : ComposeAttr()

internal class RecommendViewEvent : ComposeEvent()

internal fun ViewContainer<*, *>.RecommendView(init: InternalRecommendView.() -> Unit) {
    addChild(InternalRecommendView(), init)
}